import { getToken, setToken, listToTree } from "@/utils/common";
import { getPersonAPI, getPermmenuAPI } from "@/api/user";

export default {
  namespaced: true,

  state: {
    token: getToken(), // 用户 token
    tabName:'',
    person: [], // 用户信息
    menus: [], // 用户菜单权限数据，包含菜单、路由、按钮
    perms: [], // 用户按钮权限数据
    // 是否折叠
    isFold: false,
    role:"",// 角色
    systemName:"",// 系统名称
    hospitalName:"",// 医院名称
    userName:"",// 用户名称"
    UserInfo:{},
    
  },

  getters: {
    menuTree(state) {
      // 过滤出需要显示的且 type 不为 2
      const list = state.menus.filter((item) => item.type !== 2 && item.isShow);
      return listToTree(list);
    },
  },

  mutations: {
    resetStore(state) {
      // 重置 Vuex 中的状态
      state.token = '';
      state.tabName = '';
      state.person = [];
      state.menus = [];
      state.perms = [];
      state.isFold = false;
      state.role = '';
      state.systemName = '';
      state.hospitalName = '';
      state.userName = '';
      // state.UserInfo={};
     
    },
    setCurTabName(state,tabName){
      state.tabName=tabName;
    },
    setSystemName(state, payload) {
      state.systemName = payload;
    },
    setHospitalName(state, payload) {
      state.hospitalName = payload;
    },
    setRole(state, payload) {
      state.role = payload;
    },
    setUserName(state, payload) {
      state.userName = payload;
    },
    setToken(state, payload) {
      state.token = payload;
      // 写入本地存储
      setToken(payload);
    },
    setUserInfo(state, payload) {
      state.UserInfo = payload;
      
    },

    setPerson(state, payload) {
      state.person = payload;
    },

    setPermmenu(state, payload) {
      state.menus = payload.menus;
      state.perms = payload.perms;
    },
    setFold(state, payload) {
      state.isFold = payload;
    },
  },

  actions: {
    /**
     * 退出登录
     */
    logout({ commit }) {
      commit("setToken", "");
    },

    async get({ dispatch, commit, state }) {
      const [person] = await Promise.all([
        getPersonAPI(),
        // getPermmenuAPI(),
      ]);
      commit("setPerson", person.data.menus);
      commit("setSystemName", person.data.systemName);
      commit("setHospitalName", person.data.hospitalName);
      commit("setRole", person.data.role);
      commit("setUserName", person.data.name);
      // return permmenu.menus.filter((item) => item.type === 1);
    
      if(person.data.name&&localStorage.getItem('user')!==person.data.name){
        localStorage.setItem('user',person.data.name)
      }
      return person.data.menus;
    },

    /**
     * 获取个人信息
     */
    getPerson({ commit }) {
      // 调用接口
      getPersonAPI().then((result) => {
        commit("setPerson", result);
      });
    },

    /**
     * 获取个人权限
     */
    getPermmenu({ commit }) {
      // 调用接口
      getPermmenuAPI().then((result) => {
        commit("setPermmenu", result);
      });
    },
    getFold({ commit, state }) {
      commit("setFold", !state.isFold);
    },
  },
};
